<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺地址" prop="shopUrl">
        <el-input
          v-model="queryParams.shopUrl"
          placeholder="请输入店铺地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:apply:add']"
        >新增</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:apply:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:apply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="applyId" />
      <el-table-column label="店铺名称" align="center" prop="shopName" />
      <el-table-column label="logo" align="center" prop="shopLogo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.shopLogo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店铺负责人" align="center" prop="shopUser"/>
      <el-table-column label="主营产品" align="center" prop="mainProducts"/>

      <el-table-column label="店铺地址" align="center" prop="shopUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:apply:edit']"
          >店铺申请信息</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看店铺详情审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="100%"  append-to-body>
      <el-form ref="form" :model="form" :rules="rules" style="height: 800px" label-width="120px">
        <div style="float: left">
          <el-form-item label="店铺名称" prop="shopName">
            <el-input v-model="form.shopName" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="logo" prop="shopLogo">
            <image-upload v-model="form.shopLogo"/>
          </el-form-item>
          <el-form-item label="店铺负责人" prop="shopUser">
            <el-input v-model="form.shopUser" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="主营产品" prop="mainProducts">
            <el-checkbox-group v-model="form.mainProducts">
              <el-input v-model="form.mainProducts"  style="width: 200px" :disabled="true" />
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="店铺地址" prop="shopUrl">
            <el-input v-model="form.shopUrl" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="详细地址" prop="shopAddress">
            <el-input v-model="form.shopAddress" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="店铺简介" prop="shopIntroduction">
            <el-input v-model="form.shopIntroduction" type="textarea" style="width: 300px" :disabled="true"/>
          </el-form-item>
          <el-form-item label="法定经营范围" prop="businessScope">
            <el-input v-model="form.businessScope" type="textarea" style="width: 300px" :disabled="true" />
          </el-form-item>
          <el-form-item label="营业执照号" prop="licenseNumber">
            <el-input v-model="form.licenseNumber" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="营业执照电子版" prop="shopELicense">
            <image-upload v-model="form.shopELicense"/>
          </el-form-item>
        </div>
        <div class="bank" style="float: left;margin-left: 100px;">
          <el-form-item label="银行开户名" prop="bankAccountName">
            <el-input v-model="form.bankAccountName" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="银行账号" prop="bankAccount">
            <el-input v-model="form.bankAccount" style="width: 200px" :disabled="true" :maxlength="19"/>
          </el-form-item>
          <el-form-item label="开户行支行名称" prop="subBranch">
            <el-input v-model="form.subBranch" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="支行联行号" prop="lineNumber">
            <el-input v-model="form.lineNumber" style="width: 200px" :disabled="true"/>
          </el-form-item>
          <el-form-item label="法人姓名" prop="legalPersonName">
            <el-input v-model="form.legalPersonName" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="法人证件号" prop="legalPersonDocument">
            <el-input v-model="form.legalPersonDocument" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="法人身份证照片" prop="legalPersonImage">
            <image-upload v-model="form.legalPersonImage"/>
          </el-form-item>
        </div>
        <div class="corporate" style="float: left;margin-left: 100px;">
          <el-form-item label="公司名称" prop="corporateName">
            <el-input v-model="form.corporateName" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="公司地址" prop="corporateUrl">
            <el-input v-model="form.corporateUrl" style="width: 300px" :disabled="true" />
          </el-form-item>
          <el-form-item label="公司详细地址" prop="corporateAddress">
            <el-input v-model="form.corporateAddress" type="textarea" style="width: 300px" :disabled="true" />
          </el-form-item>
          <el-form-item label="员工总数" prop="corporateCount">
            <el-input v-model="form.corporateCount" style="width: 200px" :disabled="true" />人
          </el-form-item>
          <el-form-item label="公司电话" prop="corporatePhone">
            <el-input v-model="form.corporatePhone" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="注册资金" prop="registeredCapital">
            <el-input v-model="form.registeredCapital" style="width: 200px" :disabled="true" />万元
          </el-form-item>
          <el-form-item label="联系人姓名" prop="contactName">
            <el-input v-model="form.contactName" style="width: 200px" :disabled="true" />
          </el-form-item>
          <el-form-item label="联系人电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" style="width: 200px" :disabled="true"/>
          </el-form-item>
          <el-form-item label="公司邮箱" prop="contactEmail">
            <el-input v-model="form.contactEmail" style="width: 200px" :disabled="true"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">通 过</el-button>
        <el-button type="danger" @click="cancel">驳 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply } from "@/api/shop/apply";
import {addShop} from "@/api/shop/shop";
import { MessageBox } from 'element-ui';
export default {
  name: "Apply",
  dicts: ['main_products_main_products'],
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺审核表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      danger:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: null,
        shopLogo: null,
        mainProducts: null,
        shopUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        shopName: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" }
        ],
        shopAddress: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询店铺审核列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 驳回按钮
   async cancel() {
      try {
        const danger = await MessageBox.prompt('请填写审核失败的理由：', '审核失败', {
          inputPlaceholder: '请输入失败理由',
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(o=>{
          console.info(o.value)
          this.$refs["form"].validate(valid => {
            if (valid) {
              if (this.form.applyId != null) {
                updateApply(this.form).then(response => {
                  this.$modal.msgSuccess("审核驳回");
                  this.open = false;
                  this.getList();
                });
              }
              //驳回理由
              this.form.rejectReason=o.value
              //驳回状态为4:审核不通过
              this.form.shopStatus=4
              //放入到 shop 表中
              addShop(this.form).then(response =>{
                //从apply表中删除该申请信息
                delApply(this.form.applyId)
                this.reset()
                this.getList();
              })
            }
          });
        })
      } catch (error){
        console.info(error);
        this.reset()
        this.getList();
      }
    },
    // 表单重置
    reset() {
      this.form = {
        applyId: null,
        shopName: null,
        shopLogo: null,
        shopUser:null,
        mainProducts: null,
        shopUrl: null,
        shopAddress: null,
        shopIntroduction: null,
        bankAccountName: null,
        bankAccount: null,
        subBranch: null,
        lineNumber: null,
        legalPersonName: null,
        legalPersonDocument: null,
        legalPersonImage: null,
        corporateName: null,
        corporateUrl: null,
        corporateAddress: null,
        corporateCount: null,
        corporatePhone: null,
        registeredCapital: null,
        contactName: null,
        contactPhone: null,
        contactEmail: null,
        licenseNumber: null,
        businessScope: null,
        storeELicense: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.applyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /*/!** 新增按钮操作 *!/
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺审核";
    },*/
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const applyId = row.applyId || this.ids
        getApply(applyId).then(response => {
          this.form = response.data;
          console.info(this.form)
          /*this.form.mainProducts = this.form.mainProducts.split(",");*/
          this.open = true;
          this.title = "查看店铺信息";
        });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.applyId != null) {
            updateApply(this.form).then(response => {
              this.$modal.msgSuccess("审核通过");
              this.open = false;
              this.getList();
            });
          }
          //审核通过状态为1:审核通过
          this.form.shopStatus=1
          //审核通过时间
          this.form.shopApprovedAt = new Date();
          //放入到 shop 表中
          addShop(this.form).then(response =>{
            //从apply表中删除该申请信息
             delApply(this.form.applyId)
             this.reset()
            this.getList();
          })

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const applyIds = row.applyId || this.ids;
      this.$modal.confirm('是否确认删除店铺审核编号为"' + applyIds + '"的数据项？').then(function() {
        return delApply(applyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/apply/export', {
        ...this.queryParams
      }, `apply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
</style>
